/**
 * Copyright by 山西大昌集团 (c) 2018 DaChang Inc.
 * Date:2018年9月7日下午4:09:36
 * @author : LuNanTing
 * @Description : TODO
 */
package com.hikvision.cms.api.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

import com.hikvision.cms.api.eps.beds.EventDis;
import com.hikvision.cms.api.eps.beds.PmsEvent;
import com.hikvision.cms.api.object.entity.ParseEntity;
import com.hikvision.cms.api.object.entity.PmsEventEntity;

/**
 * @author : LuNanTing
 * @Title : OctToString.java
 */
public class OctToString {

	public static ParseEntity OctStrtoStringForCommEvent(EventDis.CommEventLog parseFrom) {
		ParseEntity parseEntity = new ParseEntity();

		/** 日志ID */
		if (!parseFrom.getLogId().isEmpty()) {
			parseEntity.setLogId(parseFrom.getLogId());
		}
		/** 事件状态 */
		if (!Integer.toString(parseFrom.getEventState()).isEmpty()) {
			parseEntity.setEventState(parseFrom.getEventState());
		}
		/** 事件等级 */
		if (!Integer.toString(parseFrom.getEventLevel()).isEmpty()) {
			parseEntity.setEventLevel(parseFrom.getEventLevel());
		}
		/** 控制中心编码 */
		if (!parseFrom.getUnitIdx().isEmpty()) {
			parseEntity.setUnitIdx(parseFrom.getUnitIdx());
		}
		/** 事件类型 */
		if (!Integer.toString(parseFrom.getEventType()).isEmpty()) {
			parseEntity.setEventType(parseFrom.getEventType());
		}
		/** 事件类型名称 */
		if (!parseFrom.getEventTypeName().isEmpty()) {
			parseEntity.setEventTypeName(parseFrom.getEventTypeName());
		}
		/** 事件所属子系统类型 */
		if (!Integer.toString(parseFrom.getSubSysType()).isEmpty()) {
			parseEntity.setSubSysType(parseFrom.getSubSysType());
		}
		/** 事件名称 */
		if (!parseFrom.getEventName().isEmpty()) {
			parseEntity.setEventName(parseFrom.getEventName());
		}
		/** 事件开始时间 */
		if (!parseFrom.getStartTime().isEmpty()) {
			parseEntity.setStartTime(parseFrom.getStartTime());
		}
		/** 事件结束时间 */
		if (!parseFrom.getStopTime().isEmpty()) {
			parseEntity.setStopTime(parseFrom.getStopTime());
		}
		/** 事件源编号 */
		if (!parseFrom.getSourceIdx().isEmpty()) {
			parseEntity.setSourceIdx(parseFrom.getSourceIdx());
		}
		/** 事件源类型 */
		if (!Integer.toString(parseFrom.getSourceType()).isEmpty()) {
			parseEntity.setSourceType(parseFrom.getSourceType());
		}
		/** 事件源名称 */
		if (!parseFrom.getSourceName().isEmpty()) {
			parseEntity.setSourceName(parseFrom.getSourceName());
		}
		/** 事件描述信息 */
		if (!parseFrom.getLogTxt().isEmpty()) {
			parseEntity.setLogTxt(parseFrom.getLogTxt());
		}
		/** 事件源区域编号 */
		if (!parseFrom.getRegionIdx().isEmpty()) {
			parseEntity.setRegionIdx(parseFrom.getRegionIdx());
		}
		return parseEntity;
	}

	/**
	 * @title 解析具体停车场信息
	 * @param parseFrom
	 * @return
	 * @author : LuNanTing
	 */
	public static PmsEventEntity OctStrtoStringForPmsEvent(PmsEvent.MsgPmsEvent parseFrom) {
		PmsEventEntity pmsEventEntity = new PmsEventEntity();
		/** 事件命令值 */
		if (!Integer.toString(parseFrom.getPmsEvent().getEventCmd()).isEmpty()) {
			pmsEventEntity.setEventCmd(parseFrom.getPmsEvent().getEventCmd());
		}
		/** 停车场名称 */
		if (!parseFrom.getPmsEvent().getParkName().isEmpty()) {
			pmsEventEntity.setParkName(parseFrom.getPmsEvent().getParkName());
		}
		/** 停车场编号 */
		if (!parseFrom.getPmsEvent().getParkIndex().isEmpty()) {
			pmsEventEntity.setParkIndex(parseFrom.getPmsEvent().getParkIndex());
		}
		/** 出入口名称 */
		if (!parseFrom.getPmsEvent().getGateName().isEmpty()) {
			pmsEventEntity.setGateName(parseFrom.getPmsEvent().getGateName());
		}
		/** 出入口编号 */
		if (!parseFrom.getPmsEvent().getGateIndex().isEmpty()) {
			pmsEventEntity.setGateIndex(parseFrom.getPmsEvent().getGateIndex());
		}
		/** 车道名称 */
		if (!parseFrom.getPmsEvent().getRoadwayName().isEmpty()) {
			pmsEventEntity.setRoadwayName(parseFrom.getPmsEvent().getRoadwayName());
		}
		/** 车道编号 */
		if (!parseFrom.getPmsEvent().getRoadwayIndex().isEmpty()) {
			pmsEventEntity.setRoadwayIndex(parseFrom.getPmsEvent().getRoadwayIndex());
		}
		/** 车辆类型 */
		if (!Integer.toString(parseFrom.getPmsEvent().getVehicleType()).isEmpty()) {
			pmsEventEntity.setVehicleType(parseFrom.getPmsEvent().getVehicleType());
		}
		/** 车牌类型 */
		if (!Integer.toString(parseFrom.getPmsEvent().getPlateType()).isEmpty()) {
			pmsEventEntity.setPlateType(parseFrom.getPmsEvent().getPlateType());
		}
		/** 车辆颜色 */
		if (!Integer.toString(parseFrom.getPmsEvent().getVehicleColor()).isEmpty()) {
			pmsEventEntity.setVehicleColor(parseFrom.getPmsEvent().getVehicleColor());
		}
		/** 车牌颜色 */
		if (!Integer.toString(parseFrom.getPmsEvent().getPlateColor()).isEmpty()) {
			pmsEventEntity.setPlateColor(parseFrom.getPmsEvent().getPlateColor());
		}
		/** 车牌号 */
		if (!parseFrom.getPmsEvent().getPlateNo().isEmpty()) {
			pmsEventEntity.setPlateNo(parseFrom.getPmsEvent().getPlateNo());
		}
		/** 事件编号 */
		if (!parseFrom.getPmsEvent().getEventIndex().isEmpty()) {
			pmsEventEntity.setEventIndex(parseFrom.getPmsEvent().getEventIndex());
		}
		/** 车道类型 */
		if (!Integer.toString(parseFrom.getPmsEvent().getRoadwayType()).isEmpty()) {
			pmsEventEntity.setRoadwayType(parseFrom.getPmsEvent().getRoadwayType());
		}
		/** 车辆主品牌 */
		if (!Integer.toString(parseFrom.getPmsEvent().getMainLogo()).isEmpty()) {
			pmsEventEntity.setMainLogo(parseFrom.getPmsEvent().getMainLogo());
		}
		/** 子品牌 */
		if (!Integer.toString(parseFrom.getPmsEvent().getSubLogo()).isEmpty()) {
			pmsEventEntity.setSubLogo(parseFrom.getPmsEvent().getSubLogo());
		}
		/** 车辆子品牌年款 */
		if (!Integer.toString(parseFrom.getPmsEvent().getSubModel()).isEmpty()) {
			pmsEventEntity.setSubModel(parseFrom.getPmsEvent().getSubModel());
		}
		/** 车牌置信度 */
		if (!Integer.toString(parseFrom.getPmsEvent().getPlateBelieve()).isEmpty()) {
			pmsEventEntity.setPlateBelieve(parseFrom.getPmsEvent().getPlateBelieve());
		}
		/** 黑名单 */
		if (!Integer.toString(parseFrom.getPmsEvent().getAlarmCar()).isEmpty()) {
			pmsEventEntity.setAlarmCar(parseFrom.getPmsEvent().getAlarmCar());
		}
		return pmsEventEntity;
	}

	public static void OctStrtoString1(EventDis.CommEventLog parseFrom) {
		ParseEntity parseEntity = new ParseEntity();
		Class comClass = parseFrom.getClass();
		Class parseClass = parseEntity.getClass();
		Field[] comfields = comClass.getDeclaredFields();
		Field[] parseFields = parseClass.getDeclaredFields();
		System.out.println("fields:" + Arrays.toString(comfields));
		for (int i = 0; i < comfields.length; i++) {
			// 先判断消息字段有无get方法,无get方法直接剔除
			Method method = getGetMethod(comClass, comfields[i].getName());
			System.out.println("字段值：" + comfields[i]);
			if (method != null) {
				// 判断某个消息字段是否在对象中
				if (Arrays.binarySearch(parseFields, comfields[i].getName()) == 1) {

					Method method1 = getGetMethod(comClass, comfields[i].getName());
					// getSetMethod(parseClass, fieldName, field)
					try {
						Object getValue = method1.invoke(parseFrom, new Object[0]);

					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				;
			}
		}
		if (!parseFrom.getLogId().isEmpty()) {
			parseEntity.setLogId(parseFrom.getLogId());
		}
	}

	/**
	 * java反射bean的get方法
	 * 
	 * @param objectClass
	 * @param fieldName
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Method getGetMethod(Class objectClass, String fieldName) {
		if (Object.class.equals(objectClass)) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("get");
		sb.append(fieldName.substring(0, 1).toUpperCase());
		sb.append(fieldName.substring(1));
		try {
			Method method = objectClass.getMethod(sb.toString());
			if (method == null) {
				method = getGetMethod(objectClass.getSuperclass(), fieldName);
			}
			return method;
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * java反射bean的set方法
	 * 
	 * @param objectClass
	 * @param fieldName
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Method getSetMethod(Class objectClass, String fieldName, Field field) {
		if (Object.class.equals(objectClass)) {
			return null;
		}
		try {
			Class[] parameterTypes = new Class[1];
			// Field field = objectClass.getDeclaredField(fieldName);
			// Field field = getFieldFromClassByName(objectClass, fieldName);
			parameterTypes[0] = field.getType();
			StringBuffer sb = new StringBuffer();
			sb.append("set");
			sb.append(fieldName.substring(0, 1).toUpperCase());
			sb.append(fieldName.substring(1));
			Method method = objectClass.getMethod(sb.toString(), parameterTypes);
			if (method == null) {
				method = getSetMethod(objectClass.getSuperclass(), fieldName, field);
			}
			return method;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
